﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework;

namespace Brain.IO
{
    public class BrainImage
    {
        private byte[] textureData;
        private int width;
        private int height;

        public byte[] TextureData
        {
            get
            {
                return textureData;
            }
        }

        public int Width
        {
            get { return width; }
            set
            {
                if (texture != null)
                {
                    texture.Dispose();
                }
                texture = null;
                width = value;
            }
        }
        public int Height
        {
            get { return height; }
            set
            {
                if (texture != null)
                {
                    texture.Dispose();
                }
                texture = null;
                height = value;
            }
        }

        private Texture2D texture;
        public Texture2D Texture2D
        {
            get
            {
                return texture;
            }
        }

        public BrainImage()
        {
        }

        public void SetTexture(Texture2D Tex)
        {
            texture = Tex;
            width = Tex.Width;
            height = Tex.Height;
        }
        public void GenerateTexture()
        {
            texture = new Texture2D(Engine.Instance.GraphicsDevice, width, height);
            texture.SetData<byte>(textureData);
        }

        #region Serializing

        /// <summary>
        /// Saves the Image
        /// </summary>
        /// <param name="Image">Image to be saved</param>
        /// <param name="FileName">Place to save it</param>
        public static void Save(BrainImage Image, Stream stream)
        {
            BinaryWriter Writer = new BinaryWriter(stream);
            Writer.Write(Image.Width);
            Writer.Write(Image.Height);
            Writer.Write(Image.TextureData, 0, Image.TextureData.Length);

            Writer.Dispose();
        }
        public static BrainImage Load(Stream stream, BrainImageLoadOptions Options)
        {
            BrainImage Image = null;
            BinaryReader Reader = new BinaryReader(stream);
            int width = Reader.ReadInt32();
            int height = Reader.ReadInt32();
            byte[] info = Reader.ReadBytes(4 * width * height);

            if (Options != null && Options.ColorKeyEnabled)
            {
                Microsoft.Xna.Framework.Color col = Options.ColorKeyColor;
                for (int i = 0; i < info.Length; i += 4)
                {
                    if (info[i] == col.R &&
                        info[i + 1] == col.G &&
                        info[i + 2] == col.B &&
                        info[i + 3] == col.A)
                    {
                        info[i] = 0;
                        info[i + 1] = 0;
                        info[i + 2] = 0;
                        info[i + 3] = 0;
                    }
                }
            }

            Texture2D texture = new Texture2D(Engine.Instance.GraphicsDevice, width, height);
            texture.SetData<byte>(info);
            Image = new BrainImage();
            Image.SetTexture(texture);

            Reader.Dispose();
         
            return Image;
        }


        #endregion
    }
}
